<?php
// subscription prices page
//require_once 'view/adm/users/prices.index.php';
require_once WACT_ROOT . 'controller/controller.inc.php';
require_once WACT_ROOT . 'controller/form.inc.php';
require_once WACT_ROOT . 'validation/rules/match.inc.php';
require_once WACT_ROOT . 'validation/rules/member.inc.php';
require_once WACT_ROOT . 'validation/rules/pattern.inc.php';

require_once WACT_ROOT .'view/formview.inc.php';
require_once APP_ROOT . 'view/common/layout.formview.php';
require_once APP_ROOT . 'model/common/categories.php';
require_once APP_ROOT . 'view/adm/content/categories.index.php';


class CategoriesIndexPage extends PageController {

	var $form;

    function CategoriesIndexPage() {
        parent::PageController();
        $this->form =& new FormController();
        $this->form->addChild('save', new ButtonController(new Delegate($this, 'save')));
        $this->form->addChild('create', new ButtonController(new Delegate($this, 'create')));
        $this->form->addChild('clean', new ButtonController(new Delegate($this, 'clean')));
        $this->form->setDefaultChild('create');


             $this->form->addRule(new Handle('RequiredRule', array('name')));



        $this->addChild('CategoryForm', $this->form);
        $this->setDefaultChild('CategoryForm');
        $this->setDefaultView(new Handle('CategoriesListView', array('/adm/content/categories.index.html')));

        $this->addView('success', WACT_DEFAULT_VIEW);

        $this->form->registerOnLoadListener(new Delegate($this, 'load'));
        //$this->registerOnActivateListener(new Delegate($this, 'guard'));

    }

    function clean(&$source, &$request, &$responseModel) {
        $responseModel->removeAll();
        $responseModel->set('supress_errors', 1);
    	return 'success';
    }

    function load(&$source, &$request, &$responseModel) {
        $category_id = $request->getParameter('category_id');

     	if ($category_id ) {
        	$record =& CategoriesModel::getRecord($category_id);
        	if (is_null($record)) {
            	return 'notfound';
        	}
            $responseModel->import($record->export());

        }

        return 'success';
    }
    function save(&$source, &$request, &$responseModel) {
        if ($responseModel->isValid()) {
        	$category_id = $request->getParameter('category_id');

            if ($category_id) CategoriesModel::updateCategory($category_id, $responseModel);
            $this->saveLinkedCategories($source, $request, $responseModel);
            $responseModel->removeAll();
            return 'success';
        }

        return 'success';

    }

    function create(&$source, &$request, &$responseModel) {
        if ($responseModel->isValid()) {
            require_once WACT_ROOT.'/util/phpcompat/clone.php';

            $category_id = CategoriesModel::insertCategory($responseModel);
            if ($category_id) {
            	$responseModel->set('category_id', $category_id);
            	$this->saveLinkedCategories($source, $request, $responseModel);

                $lastRecord = clone_obj($responseModel);

                // Set up the model for adding the next record
                $responseModel->removeAll();
                $responseModel->set('LastRecord', $lastRecord);

                return 'success';
            } else {
                return 'failure';
            }
        } else {

        }
        return 'success';

    }

     function saveLinkedCategories(&$source, &$request, &$responseModel) {
     	$categories1 = $responseModel->get('linked1');
        $categories2 = $responseModel->get('linked2');
        $type = $responseModel->get('type');
        if ($type==1) $categories =& $categories2;
        else if ($type==2) $categories =& $categories1;
        else return;

     	$category_id =  $responseModel->get('category_id');

        $output = array();

        // process normal permissions
        foreach($categories as $index=>$linked_category_id) {
        	$row = array('category_id'=>$category_id, 'linked_category_id'=>$linked_category_id);
       		$output[$linked_category_id] = $row;
        }

        CategoriesModel::saveLinkedcategories($output, $category_id);

     }
}

?>